package com.wasu.constant;


import android.os.Build;
import android.text.TextUtils;

import com.wasu.sdk_ott.utils.WasuTvModelUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;


public class Config {

    /**
     * 用户分组Code
     */
    public static String  userGroupId = "";
    /**
     * 测试域名（url）开关，默认关闭使用正式域名
     */
    public static boolean debugDomainSwitch = false;

    /**
     * 修改stbid
     */
    public static String customedStdId = null;

    /**
     * 保存测试url的sp文件名
     */
    public static final String PREFERENCE_DOMAIN_DEBUG = "domain_debug";
    public volatile static String sUserId;
    public volatile static String key;
    public volatile static String uid;
    public static String sUserProfile;
    public static String customerid;
    public static final String PREFERENCE_NAME = "auth-inf";
    public static final String SP_FILE_NAME = "wasu_set";

    public static String getRegion() {
        if (sUserProfile != null) {
            String[] arrayOfString = sUserProfile.split("#");
            if (arrayOfString.length > 1) {
                return arrayOfString[0] + "#" + arrayOfString[1];
            }
        }
        return "";
    }

    public static String getStbid() {
        return WasuTvModelUtil.wasu_bsa_getTvid();
    }

    public static String getBossId() {
        if (sUserProfile != null) {
            String[] arrayOfString = sUserProfile.split("#");
            if (arrayOfString.length > 1) {
                return arrayOfString[0];
            }
        }
        return getDefaultBossId();
    }

    /**
     * 获取默认的BossId
     * 9.0 200E盒子默认是杭网
     * @return
     */
    public static String getDefaultBossId(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            return "01";
        }
        if(WasuTvModelUtil.model_wasu_value.startsWith("WASU_4K_TVOS")){//省网
            return "03";
        }else if(WasuTvModelUtil.model_wasu_value.startsWith("WASU_4K")){//杭网
            return "01";
        } else{
            return "01";
        }
    }

    /**
     * 获取默认的UserProfile
     * 9.0 200E盒子默认是杭网
     * @return
     */
    private static boolean getDefaultModel(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            return true;
        }
        if(WasuTvModelUtil.model_wasu_value.startsWith("WASU_4K_TVOS")){//省网
            return false;
        }else if(WasuTvModelUtil.model_wasu_value.startsWith("WASU_4K")){//杭网
            return true;
        } else{
            return true;
        }
    }
    public static boolean isHz() {
        String bossid =getBossId();
        if("01".equals(bossid)) {
            return true;
        }
        else if("03".equals(bossid)) {
            return false;
        }
        else {
            return getDefaultModel();
        }
    }
    private static String buildModel = null;
    public static synchronized String readDvbOttModel() {
        if (buildModel == null || TextUtils.isEmpty(buildModel)) {
            buildModel = readTCLBuildModelSpecial();
        }
        return buildModel;
    }
    public static String readTCLBuildModelSpecial() {
        String fileName = "data/devinfo.txt";
        File f = new File(fileName);
        if (!f.exists()) {
            String model = WasuTvModelUtil.model_wasu_value;
            if (model.equalsIgnoreCase(WasuTvModelUtil.UNKNOWN)) {
                return Build.MODEL;
            } else {
                return model;
            }
        }
        String buildModel = null;
        try (BufferedReader in = new BufferedReader(new FileReader(f))) {
            String line = in.readLine();
            while (line != null) {
                line = line.trim();
                if (line.startsWith("devmodel")) {

                    int index = line.indexOf("=");
                    buildModel = line.substring(index + "=".length());
                    break;
                }
                line = in.readLine();
            }
        } catch (IOException e) {
            buildModel = null;
        }
        if (buildModel == null) {
            return "";
        } else {
            return buildModel;
        }
    }

    private static String MAC_ETH0 = null;
    public synchronized static String wasu_bsa_getMacEth0() {
        if (MAC_ETH0 == null || MAC_ETH0.equalsIgnoreCase("000000000000")) {
            MAC_ETH0 = getMacEth0();
        }
        return MAC_ETH0.toUpperCase();
    }
    /**
     * 获取手机 th0 mac地址<br/>
     * 错误返回12个0
     */
    public static String getMacEth0() {
        String macSerial = null;
        String macAddress = null;
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            if ((macAddress = input.readLine()) != null) {
                macSerial = macAddress.trim();
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        if (macSerial != null) {
            return macSerial.replaceAll(":", "");
        } else {
            return "000000000000";
        }
    }

}
